#!/bin/bash
# 每半小时 执行
if [ -z "$1" ] ; then
    deal_date=$(date -d "1 day ago" +%Y-%m-%d)
else
    deal_date=$1
fi

if [ -z "$2" ] ; then
    deal_type='cpa'
else
    deal_type=$2
fi


LOG_DIR="/home/cys/www/script/logs"
# 日志文件名包含日期，每次执行的日志前添加时间戳
FILE_NAME="sync_online_statistics_data"
LOG_FILE="${LOG_DIR}/${FILE_NAME}_$(date +%Y-%m-%d).log"
mkdir -p "${LOG_DIR}"
echo "" >> "${LOG_FILE}"
echo "" >> "${LOG_FILE}"
# 记录开始时间
echo "[$(date +%Y-%m-%d_%H:%M:%S)] 开始执行 同步任务 命令" >> "${LOG_FILE}"
echo "处理日期：$deal_date" deal_type:${deal_type} >> "${LOG_FILE}"

# 验证日期合法性（通过date命令解析，失败则报错）
if ! date -d "$deal_date" >/dev/null 2>&1; then
  echo "错误：输入的日期 $deal_date 不合法" >> "${LOG_FILE}"
  exit 1
fi



if [[ "$deal_type" = "cpa" ]]; then
   table_final_name="t_data_user_cpa_new_final"
   table_deduction_name="t_data_user_cpa_new_deduction"
   anch_key="stat.ch.new.anchor"
else
   table_final_name="t_data_user_device_new_final"
   table_deduction_name="t_data_user_device_new_deduction"
   anch_key="stat.ch.new.device.anchor"
fi

echo "删除${table_final_name}表 $deal_date 天数据" >> "${LOG_FILE}"

clickhouse-client --password Keepgo123@cys -q "
alter table  db_cfx.${table_final_name}  delete where activate_date ='${deal_date}';
"

#echo "删除${table_deduction_name}表 $deal_date 天数据" >> "${LOG_FILE}"

#clickhouse-client --password Keepgo123@cys -q "
#alter table  db_cfx.${table_deduction_name}  delete where activate_date ='${deal_date}'  and channel_name!='lockscreen_false'
#"
sleep 2
echo "同步线上表数据" >> "${LOG_FILE}"

if [[ "$deal_type" = "cpa" ]]; then
  clickhouse-client --password Keepgo123@cys -q "
   insert into db_cfx.t_data_user_cpa_new_final (user_id,channel_name,channel,dev_brand,dev_model,os_version,os_incremental,sdk_version,oaid,vn,vc,gaid,maid,activate_time,activate_ip,activate_area,status)
   select user_id,channel_name,channel,dev_brand,dev_model,os_version,os_incremental,sdk_version,oaid,vn,vc,gaid,maid,fromUnixTimestamp64Milli(activate_time) AS activate_time1,activate_ip,activate_area,status
   from db_cfx.t_mysql_data_user_device_valid_active
   where valid_type =1 and  toDate(activate_time/1000)='${deal_date}'
   " 2>> "${LOG_FILE}"
else
   clickhouse-client --password Keepgo123@cys -q "
   insert into
   db_cfx.t_data_user_device_new_final (user_id,channel_name,channel,dev_brand,dev_model,os_version,os_incremental,sdk_version,oaid,vn,vc,gaid,maid,activate_time,activate_ip,activate_area,status,carousel_status,carousel_close_time,lockscreen_status,lockscreen_close_time,valid_type)
   select user_id,channel_name,channel,dev_brand,dev_model,os_version,os_incremental,sdk_version,oaid,vn,vc,gaid,maid,fromUnixTimestamp64Milli(activate_time) AS activate_time1,activate_ip,activate_area,status,carousel_status,carousel_close_time,lockscreen_status,lockscreen_close_time,valid_type
   from db_cfx.t_mysql_data_user_device_active
   where valid_type =1 and toDate(activate_time/1000)='${deal_date}'
   " 2>> "${LOG_FILE}"
fi



echo "更新 mysql stat_anchor表 ${anch_key}数据" >> "${LOG_FILE}"

#sh /home/cys/www/script/mysql_tools/update_mysql_stat_anchor.sh ${anch_key} ${deal_date}

echo "[$(date +%Y-%m-%d_%H:%M:%S)]  $deal_date 同步数据处理结束" >> "${LOG_FILE}"

